/**
 * Dans.com Inc. Copyright (c) 2011-2020 All Rights Reserved
 */
package com.example.mqreceiver;


import enums.EventTypeEnum;
import event.BusinessEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ke
 * Created by on 2020-10-24 14:02
 */
@Component
public class MqEventProcessorFactory {

    @Autowired
    private List<MqEventProcessor> MqEventProcessors;
    @Autowired
    private List<BusinessEvent> dicEvents;

    private Map<String, MqEventProcessor> eventProcessorMap;

    private Map<String, BusinessEvent> eventMap;

    @PostConstruct
    public void init() {
        if (CollectionUtils.isEmpty(MqEventProcessors)) {
            return;
        }
        if (CollectionUtils.isEmpty(dicEvents)) {
            return;
        }
        eventProcessorMap = new HashMap<>(8);
        eventMap = new HashMap<>(8);
        for (MqEventProcessor processor : MqEventProcessors) {
            eventProcessorMap.put(processor.getEventType().getType(), processor);
        }
        for (BusinessEvent processor : dicEvents) {
            eventMap.put(processor.getEventType().getType(), processor);
        }
    }

    public MqEventProcessor getEventProcessor(String eventType) {
        EventTypeEnum eventTypeEnum = EventTypeEnum.getByType(eventType);
        if (null == eventTypeEnum) {
            return null;
        }

        return eventProcessorMap.get(eventType);
    }
    public BusinessEvent getEvent(String eventType) {
        EventTypeEnum eventTypeEnum = EventTypeEnum.getByType(eventType);
        if (null == eventTypeEnum) {
            return null;
        }

        return eventMap.get(eventType);
    }
}
